Welcome back.
In this lecture we're going to take a look at a neat little function in the string library called string
dot format.
The format function allows us to format strings into different formats that we desire.
As an example, let's say we wanted to be able to get a string that is formatted in a month slash day
slash year format, and we can just simply take numbers like five for the month, three for the day
and 2015 for the year, and convert it into a string that says something like 05/03/ 2015.
We can do exactly that using the format function.
So the format function takes one argument and that's a string.
And inside of that string you have to place some things in there called directives.
And these directives tell the function how to format the string.
These directives act as placeholders and will be replaced by whatever else you provide to the format
function.
So what is a directive?
Well, a directive is defined by putting a percentage symbol in a string and then following up with
any flags a width parameter, a precision parameter and a specifier.
This whole collective statement itself is called the directive.
Now when you place these directives in a string, you're telling the formatting function to expect another
argument that is going to replace the directive.
Now, you can use these directives to insert other strings into a string and certain numbers, decimals,
and many other values.
So let's first take a look at the width parameter.
Now this parameter is optional and it defines how many spaces in a string that a value that is going
to replace the directive is going to take up.
So for example, if I did something like percentage and three s, I'm saying that the string that is
going to take up this directive is going to be three characters in length, and it should be placed
right here.
If the string is greater than three characters, then it will just put the whole string.
However, if the string is less than three characters, the remaining unused space will be replaced
with an empty space.
So for example, if I pass a string to it that just says hi, then what gets replaced here at this directive
will be a string that has a space and then hi, which totals three characters.
Now the precision parameter tells the function how many decimal points we want to be placed in a string
for a float.
For example, if I wanted to put a floating point number in a string, I would put percentage F that
would stand for a floating point number that would be replaced at this directive.
However, if I only wanted to include numbers in this float up to, let's say the hundredths place,
then I would put point two.
So percentage point two F, which is telling the function to include only numbers up to the hundredth
place, or the second decimal place after the decimal point.
So enough explaining.
Let's actually go ahead and take a look at some examples.
So let's say I have two strings.
I'm going to call one some string one set it to something like hello.
And let's say I had another string called some string two and set it equal to a string like hi.
Then what I could do is I could create a formatted string, I'll call it formatted, and it's going
to be equal to the string dot format function.
And we're going to pass a string in here that says this is percentage s and percentage s.
So I've put two directives inside of the string one here and one here.
And that means I'm going to have to pass two values that's going to replace these directives.
In this case they have to be strings because I denoted them as strings.
So in the first argument I could put some string one.
And then in the second argument I could put some string two.
So some string one, which is hello is going to replace the first directive, and some string two is
going to replace the second directive.
And that means we can go ahead and print this formatted string in the console.
And we should get a string that says this is hello and hi.
So if we run the game.
There we go.
We get our string.
This is hello and hi.
And just like that, we formatted the string by inserting other strings into it.
Now we can also do this with numbers.
I'm going to create a variable.
I'll call it num formatted equal to the string dot format function.
And to define that we want to insert a number into a string, we have to use one of these specifiers
for numbers, which is percentage d.
And this is for integers or numbers.
So what we could do is I could create a string that says x is equal to percentage d.
This is my first directive.
And then I could do y is equal to percentage d.
And then I have to pass those numbers.
So for the first number I could do like 12.
And for the second number I could do negative five.
So this should give us a string that says x is equal to 12 and y is equal to negative five.
So we can go ahead and print num formatted in the console.
And we should get exactly that.
And there we go.
X is equal to 12.
Y is equal to negative five.
Now let's go ahead and take a look at the width parameter for our directive.
So I'm going to print out the return from the String.format function.
And I'm going to put a string in here that's just um, let's do three dots.
And then we can define that.
We want to put a number here.
So we'll do percentage D and then we'll put another three dots.
And then we could pass a number like ten.
So this should print dot dot dot ten dot dot dot.
Now let's say I wanted to put a bunch of empty space in front of the number ten.
Well then I could pass a width here.
I could put in a width of ten, which means whatever number is going to be replaced here, if it isn't
equal to or greater than ten characters in width.
In this case, our ten is only two characters, then it's going to fill up the rest of the unused space
with empty spaces.
So when this prints out, it should print dot dot dot a bunch of spaces and then ten dot dot dot.
So let's go ahead and run and see what we get.
And there we go.
We get dot, dot, dot and then we get let's see 123456, seven, eight.
That's eight empty spaces plus one and zero.
So that's a total of ten.
So again we told the function to expect a total of ten characters to be taken up.
And since the number we passed only took up two, it filled up the rest with empty spaces.
Now let's go ahead and take a look at something else a little bit cool.
And we're going to take a look at flags.
Now I haven't explained flags yet, but what flags are is that they're an extra bonus we can add to
our directive to kind of further fine tune how we want our string to be formatted.
So, for example, let's say I didn't want all of these spaces here to be in front of the number.
Let's say I wanted the number to be first and then the empty spaces could come along well.
I could put a flag in there, which is a minus symbol, and this will force the value, whatever is
going to be replaced by this directive, whatever value it is, in this case it's ten.
It's going to force it to be on the left side instead of the right side.
So if we go and run our game, we should get there.
We go dot dot dot ten.
And then we get our eight empty spaces and then dot dot dot.
So we just move the value from the end to the beginning.
So that's pretty neat.
Now let's go ahead and take a look at an example of using the precision parameter.
So let's say I wanted to print out pi right.
Math dot pi which is 3.14159 and so on.
But let's say I only want to get the first four digits of pi.
Well, what I could do is I'll create a variable, I'll just call it pi, and it'll be equal to string.format.
And we'll put a string in here that says pi is equal to.
And I'll put our directive in here.
So we could do percentage.
And again we want to define the precision here.
So we want to go up to let's say the fourth decimal spot.
So we could do 0.4.
So it'll give us four digits after the decimal point.
And then we'll make sure to put F here to denote this as a floating point number that we're going to
be replacing right here at this directive.
And then we can pass math dot pi.
And this should print in the console three point and then four digits of pi.
So let's go ahead and print out pi here.
And there we go.
We get 3.16.
So it rounded up pi for us.
But as you can see we only got the first four digits.
If I wanted to get more than that, then I could define something like maybe eight and we'll get eight
digits after the decimal point.
There we go.
Eight digits after the decimal point.
So as you can see, the precision parameter isn't too complex.
It just lets us decide how many numbers after the decimal point we want.
For this particular floating point number.
Now, that example I gave originally about formatting a date.
Let's go ahead and do that right now.
So I'm going to create a variable I'm going to call it date.
Let me scroll this down here.
And it's going to be equal to string dot format.
And then we can format our date here.
Now there's another cool flag we can use, which is a zero.
And I've defined it up here.
It says pads the value on the left side with zeros.
So basically remember that when we defined the width of our string it just filled it up with a bunch
of empty spaces.
Well if we put a zero there as the flag, all of the empty spaces will be instead replaced with zeros.
Yep, that's all it does.
So that means what we could do is we could put a directive here like percentage and pass zero, meaning
that any empty space will be filled with zeros.
And then we can define the width.
So for the days we only know the days go up to two digits.
That means we can put two here.
And then we'll specify it as a number with D.
And then we could put a slash here and do the same thing for the days.
So 02D.
Well actually depending on how you like your calendar set up this could be months or days.
And this could be months or days.
And then for the last one it's going to be the year.
And we want four digits.
So we can just do percentage 04D which isn't really necessary because if you pass something like 19
then it's going to say zero zero 19, even though you might want like 2019.
But that's not an issue for us right now.
But now what we could do is we could pass three values that are going to replace each of these directives.
So let's say for the month, I want it to be the third month for the day.
It could be the first.
And then for the year I could do something like 2013.
And now, because we've defined that we want to fill up the empty spaces with zero.
When we pass three here, it's going to see, hey, this is only taking up one space.
Even though we defined it to take up two spaces, that means the empty space is going to be filled with
a zero, and that means it's going to be formatted as zero three.
Same thing for the day.
It will be formatted as zero one.
And since 2013 takes up four spaces.
Then it isn't going to put in any zeros for us.
And it should just say 2013.
So let's go ahead and print this date out into the console and we should get 0301 2013.
And there we go.
Just like that, we get 0301 2013 so it filled up that empty space with a zero.
Did the same here.
And then since this number filled up all of those four spaces that we defined, it didn't put any extra
zeros in there.
So that is the neat little power of the string dot format function.
If you want to learn more about the different flags and the different specifiers that exist for the
string format function, I'll leave an article attached to the lecture that goes much more into depth
about all of these parameters and specifiers for our directives, but for now, this should give you
a very good idea of how you can use the String.format function in your scripts.
All right, that's all for me for this lecture.
I hope you find this information useful, and maybe you'll start to use the String.format function in
your own code soon.
See you in the next one.